package org.nowcoder.leetcode.twoPointers;

/**
 * Title  : 206. Reverse Linked List
 * Source : https://leetcode.cn/problems/reverse-linked-list/description/
 * Author : XrazYang
 * Date   : 2023-10-21
 */

public class LeetCode_206 {
    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode reverseList(ListNode head) {
        if (head != null) {
            ListNode p = head, q = head.next;
            while (q != null) {
                ListNode next = q.next;
                q.next = p;
                if (p == head) {
                    p.next = null;
                }
                p = q;
                q = next;
            }
            head = p;
        }
        return head;
    }
}
